/**
 * ...
 * @author Franco Ponticelli
 */

package uform.ui.comp;
import uform.property.IProperty;
import uform.property.Property;
import uform.ui.comp.Container;
import uform.ui.comp.StackItem;
import uform.ui.comp.Component;
import hxevents.Dispatcher;
using Types;

class ReadStack extends Container
{
	public var onInsert(default, null) : Dispatcher<Int>;
	public var onRemove(default, null) : Dispatcher<{ item : StackItem, pos : Int }>;

	var list : Array<StackItem>;
	public var length(default, null) : Int;
	public function new()
	{
		super();
		list = [];
		length = 0;
		onInsert = new Dispatcher();
		onRemove = new Dispatcher();
	}
	
	override function _id() : String
	{
		return "T";
	}
	
	public function first() : Component
	{
		var item = firstItem();
		if (null == item)
			return null;
		else
			return item.child;
	}
	
	public function last() : Component
	{
		var item = lastItem();
		if (null == item)
			return null;
		else
			return item.child;
	}
	
	public function get(index : Int) : Component
	{
		var item = getItem(index);
		if (null == item)
			return null;
		else
			return item.child;
	}
	
	public function firstItem() : StackItem
	{
		return list[0];
	}
	
	public function lastItem() : StackItem
	{
		return list[length-1];
	}
	
	public function getItem(index : Int) : StackItem
	{
		return list[index];
	}
	
	public function exists(component : Component) : Bool
	{
		if (component.parent.isNull() || !Std.is(component.parent.get(), StackItem))
			return false;
		else
			return existsItem(cast component.parent.get());
	}
	
	public function existsItem(item : StackItem) : Bool
	{
		for (i in 0...list.length)
			if (item == list[i])
				return true;
		return false;
	}

	public function position(component : Component) : Int
	{
		var item = component.parent.get();
		if (null == item || !Std.is(item, StackItem))
			return -1;
		else
			return positionItem(cast item);
	}
	
	public function positionItem(item : StackItem) : Int
	{
		for (i in 0...list.length)
			if (item == list[i])
				return i;
		return -1;
	}
	
	public function iterator() : Iterator<StackItem>
	{
		return list.iterator();
	}
	
	override function dispose()
	{
		while (list.length > 0)
			list[0].dispose();
		super.dispose();
	}
	
	override function componentByPath(path : String) : INamedComponent {
		var children = [];
		for(c in iterator())
		{
			var child = c.child;
			var ctrl = child.as(INamedComponent);
			if(null != ctrl && ctrl.path() == path)
				return ctrl; 
			child.ifIs(Container, function(c) children.push(c));
		}
		for (container in children)
		{
			var ctrl = container.componentByPath(path);
			if (null != ctrl)
				return ctrl;
		}
		return null;
	}
	
	override function componentByName(name : String) : INamedComponent {
		var children = [];
		for(c in iterator())
		{
			var child = c.child;
			var ctrl = child.as(INamedComponent);
			if(null != ctrl && ctrl.name == name)
				return ctrl; 
			child.ifIs(Container, function(c) children.push(c));
		}
		for (container in children)
		{
			var ctrl = container.componentByName(name);
			if (null != ctrl)
				return ctrl;
		}
		return null;
	}
}